package com.cn.aam.checaiduo.ui.me;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.cn.aam.checaiduo.AppData;
import com.cn.aam.checaiduo.R;
import com.cn.aam.checaiduo.backend.RetrofitHome;
import com.cn.aam.checaiduo.backend.observer.OnRequestListener;
import com.cn.aam.checaiduo.backend.rx.RxSchedulers;
import com.cn.aam.checaiduo.base.BaseActivity;
import com.cn.aam.checaiduo.base.ToolBarManager;
import com.cn.aam.checaiduo.base.web.NormalWebActivity;
import com.cn.aam.checaiduo.bean.me.RedirectContentB;
import com.cn.aam.checaiduo.center.UserCenter;
import com.cn.aam.checaiduo.utils.MyEditTextUtils;
import com.cn.aam.checaiduo.widget.WMToast;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 提现
 */
public class WithDrawActivity extends BaseActivity {


    @BindView(R.id.tv_balance)
    TextView tvBalance;
    @BindView(R.id.tv_bank)
    TextView tvBank;
    @BindView(R.id.et_withdraw_amount)
    EditText etWithdrawAmount;


    private double balance;

    public static Intent newInstance(Context context, String balance) {
        Intent intent = new Intent(context, WithDrawActivity.class);
        intent.putExtra("balance", balance);
        return intent;
    }

    @Override
    protected int layoutId() {
        return R.layout.activity_with_draw;
    }

    @Override
    protected void initView() {
        //toolbar
        ToolBarManager.with(this, WMContent).setBackgroundColor(R.color.toolbar_bg_grey)
                .setNavigationIcon(R.drawable.ic_nav_arrow_back)
                .setTitle("提现")
                .setTitleColor(R.color.black_normal);

        String bank_cardno = UserCenter.getInstance().getUser().getBank_cardno();
        String bankName = UserCenter.getInstance().getUser().getBank_name();


        if (TextUtils.isEmpty(bankName)) {
            findViewById(R.id.rl_bank).setVisibility(View.GONE);
            return;
        }
        String balanceStr = getIntent().getStringExtra("balance");
        tvBalance.setText(balanceStr);
        tvBank.setText(bankName + "（尾号+" + bank_cardno.replace("*", "") + "）");
        balance = Double.parseDouble(balanceStr);
    }


    @OnClick(R.id.btn_submit)
    public void onClickWithDraw() {
        if (checkRechargeValue()) doWithDraw();
    }

    /**
     * 检查值
     *
     * @return 通过为 true 不通过为 false
     * @info 此处可以无限增加各种检查条件
     */
    private boolean checkRechargeValue() {
        String withAmount = MyEditTextUtils.getContent(etWithdrawAmount);
        if (TextUtils.isEmpty(withAmount)) {
            WMToast.normal("请输入提现金额");
            return false;
        }
        if (withAmount.charAt(0) == '.') {
            WMToast.normal("金额格式有误");
            return false;
        }
        double amount = Double.parseDouble(withAmount);
        if (amount > balance) {
            WMToast.normal("提现金额超过可用余额");
            return false;
        }
        return true;
    }


    /**
     * 提现操作
     */
    private void doWithDraw() {
        String withAmount = MyEditTextUtils.getContent(etWithdrawAmount);
        RetrofitHome.getInstance().getInternalApi().withdraw(AppData.getToken(), withAmount)
                .compose(RxSchedulers.<RedirectContentB>toMain())
                .subscribe(new OnRequestListener<RedirectContentB>(this) {
                    @Override
                    public void onNext(RedirectContentB value) {
                        super.onNext(value);
                        if (value.getMsg().equals("余额为空或小于等于0")) {
                            WMToast.normal("余额为空");
                            return;
                        }
                        startActivity(NormalWebActivity.newInstance(mContext, value.getRedirect_content(), "提现", "MEFRAGMENT"));
                    }
                });

    }
}
